package myhadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;

/**
 * driver类，即程序的入口类，使用hadoop框架的好处是，不用谢输入和输出文件的代码，系统会自动为你写入。
 */
public class HadoopTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf=new Configuration();
        String[] otherArgs=new GenericOptionsParser(conf,args).getRemainingArgs();

        if (otherArgs.length<2){
            System.out.println("必须定义输入和输出文件路径");
            System.exit(2);
        }
        // 不用new,操作而使用静态方法。
        JobConf jobConf=new JobConf(conf);

        FileInputFormat.addInputPath(jobConf,new Path(otherArgs[0]));
        System.out.println(otherArgs[0]);
        System.out.println(otherArgs[1]);
        FileOutputFormat.setOutputPath(jobConf,new Path(otherArgs[1]));

        Job job=Job.getInstance(jobConf,"dfs");

        job.setJarByClass(HadoopTest.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);

        System.exit(job.waitForCompletion(true)? 0:1);
    }
}
